package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 1.继承 HttpServlet
 * 2.重写doGet()/doPost()
 */
@WebServlet(urlPatterns = {"/login","/doLogin"})
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("=====>doGet");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("=====>doPost");
        //设置请求时字符编码
        req.setCharacterEncoding("UTF-8");
        //设置响应时字符编码
        resp.setContentType("text/html;charset=UTF-8");
        //实例化out
        PrintWriter out = resp.getWriter();
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if("t161".equals(username)&&"123456".equals(password)){
            out.print("<script>alert('登录成功！');location='index.jsp'</script>");
        }else{
            out.print("<script>alert('登录失败！！！');location='login.jsp'</script>");
        }
        out.flush();
        out.close();
    }
}
